package bl.commodity;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import po.CommodityPo;
import po.Po;
import blservice.commodity.CommodityBLService;
import vo.CommodityVo;
import vo.Vo;
import dbservice.commodity.CommodityDBService;
import dbservice.history.SystemLogDBService;

public class CommodityBL extends UnicastRemoteObject implements CommodityBLService{

	private static final long serialVersionUID = 7882788320447568486L;

	CommodityDBService commodityDB;

	@SuppressWarnings("unused")
	private SystemLogDBService log;
	public CommodityBL(CommodityDBService db,SystemLogDBService log) throws RemoteException {
		this.commodityDB=db;
		this.log=log;
	}
	
	
	
	private CommodityVo _vo(CommodityPo po){
		CommodityVo vo=new CommodityVo();
		vo.setBelongTo(po.get所属分类());
		vo.setName(po.get名称());
		vo.setstocknum(po.get库存数量());
		vo.set售价(po.get售价());
		vo.set型号(po.get型号());
		vo.set库存数量警戒值(po.get库存数量警戒值());
		vo.set最近售价(po.get最近售价());
		vo.set最近进价(po.get最近进价());
		vo.set编号(po.get编号());
		vo.set进价(po.get进价());
		vo.set库存均价(po.get库存均价());
		vo.set库存总价(po.get库存总价());
		return vo;
		
	}
	private CommodityPo _po(CommodityVo vo){
		CommodityPo po=new CommodityPo();
		po.set名称(vo.getName());
		po.set售价(vo.get售价());
		po.set型号(vo.get型号());
		po.set库存数量(vo.getstocknum());
		po.set库存数量警戒值(vo.get库存数量警戒值());
		po.set所属分类(vo.getBelongTo());
		po.set最近售价(vo.get最近售价());
		po.set最近进价(vo.get最近进价());
		po.set编号(vo.get编号());
		po.set进价(vo.get进价());
		po.set库存均价(vo.get库存均价());
		po.set库存总价(vo.get库存总价());
		return po;
	}
	
	
	
	

	public List<CommodityVo> getAll() {
		List<CommodityPo> po_list=commodityDB.getAll();
		List<CommodityVo> vo_list=new ArrayList<CommodityVo>();
		for(int i=0;i<po_list.size();i++){
			vo_list.add(_vo(po_list.get(i)));
		}
		return vo_list;
	}
	
	public Vo addCommodity(CommodityVo vo) {
		Po po=commodityDB.add(_po(vo));
		Vo rs=new Vo();
		if(po.isSuccessed()){
			rs.successed();
		}else{
			rs.failed(po.getFailMessage());
		}
		
		return rs;
	}

	public Vo deleteCommodity(CommodityVo vo) {
		Po po=commodityDB.delete(_po(vo));
		Vo rs=new Vo();
		if(po.isSuccessed()){
			rs.successed();
		}else{
			rs.failed(po.getFailMessage());
		}
		
		return rs;
	}

	public Vo updateCommodity(CommodityVo vo) {
		Po po=commodityDB.update(_po(vo));
		Vo rs=new Vo();
		if(po.isSuccessed()){
			rs.successed();
		}else{
			rs.failed(po.getFailMessage());
		}
		
		return rs;
	}
	

/*	@Override
	public CommodityVo get(CommodityVo vo) throws RemoteException {
		CommodityPo cm=commodityDB.get(_po(vo));
		return _vo(cm);
	}

	@Override
	public List<CommodityVo> getCommocityItem(CommodityVo vo)
			throws RemoteException {
		List<CommodityVo> vo_list=new ArrayList<CommodityVo>();
		List<CommodityPo> po_list=commodityDB.getCommodityItem(_po(vo));
		for(int i=0;i<po_list.size();i++){
			vo_list.add(_vo(po_list.get(i)));
		}
		return vo_list;
	}
*/
	@Override
	public List<CommodityVo> findCommodity(CommodityVo vo)
			throws RemoteException {
		List<CommodityVo> vo_list=new ArrayList<CommodityVo>();
		List<CommodityPo> po_list=commodityDB.find(_po(vo));
		for(int i=0;i<po_list.size();i++){
			vo_list.add(_vo(po_list.get(i)));
		}
		return vo_list;
	}

}
